<?php
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2018/9/27
 * Time: 下午1:53
 */

namespace app\models;
use yii\db\ActiveRecord;

class Dynamic_dynamic extends ActiveRecord {
    //获取动态列表
    public function getDynamicList($limit, $offset, $barId, $cityCode, $startTime, $endTime, $userId, $dynamicTitle,$dynamicBar,$dynamicUser,$sortType) {
        $post = Dynamic_dynamic::find()->joinWith(['user','city','bar']);
        if ($barId != -1)
            $post->andWhere(['dynamic_dynamic.bar_id' => $barId]);
        if ($cityCode != -1)
            $post->andWhere(['dynamic_dynamic.city_code' => $cityCode]);
        if ($startTime != -1) {
            $post->andWhere(['between', 'dynamic_dynamic.gmt_create', $startTime, $endTime]);
        }
        if ($userId != -1)
            $post->andWhere(['dynamic_dynamic.user_id' => $userId]);
        if ($dynamicTitle != -1)
            $post->andWhere(['or', ['like', 'user_user.user_nickname', '%'.$dynamicTitle.'%', false], ['like','dynamic_dynamic.dynamic_content', '%'.$dynamicTitle.'%', false]]);

        if($dynamicUser == 0)
            $post->andWhere(['not like', 'user_user.mobile', '168168']);
        else if($dynamicUser == 1)
            $post->andWhere(['like', 'user_user.mobile', '168168']);

        if($sortType == 0)
            $post->orderBy('dynamic_dynamic.gmt_modified DESC');
        else
            $post->orderBy('dynamic_dynamic.gmt_create DESC');

        if($dynamicBar == 0)
            $post->andWhere(['<>', 'dynamic_dynamic.bar_id', '0']);
        else if($dynamicBar == 1)
            $post->andWhere(['dynamic_dynamic.bar_id'=> '0']);

        $count = $post->count();
        $result = $post->limit($limit)->offset($offset)->asArray()->all();
        return ['total'=>$count, 'rows'=>$result];
    }

    public function getDynamic($dynamicId){
        $result = Dynamic_dynamic::find()->where(['dynamic_dynamic.dynamic_id'=>$dynamicId])
            ->joinWith(['user','bar'])->asArray()->one();
        return $result;
    }

    public function changeDynamicStatus($dynamicId,$type){
        $post = Dynamic_dynamic::findOne($dynamicId);
        $post->is_deleted = $type;
        return $post->save();
    }

    //关联用户表 获取用户信息
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }

    //关联用户表 获取用户信息
    public function getBar()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(Bar_bar::className(), ['bar_id' => 'bar_id']);
    }

    public function getCity(){
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(Gl_city::className(), ['city_code' => 'city_code']);
    }

    // 修改发布时间
    public function editGmtpublish($dynamicId, $gmtPublish) {
        $post = Dynamic_dynamic::findOne($dynamicId);
        $post->gmt_publish = $gmtPublish;

        return $post->save();
    }

    // 修改点赞、浏览
    public function editAdjustment($dynamicId, $likeCount, $browseCount) {
        $data = Dynamic_dynamic::find()->where(['dynamic_id' => $dynamicId])->asArray()->one();
        $post = Dynamic_dynamic::findOne($dynamicId);

        $post->like_count = $likeCount + $data['like_count'];
        $post->browse_count = $browseCount + $data['browse_count'];
        
        return $post->save();
    }
}